package com.scse.bms.web.client;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.scse.bms.domain.CommentVO;
import com.scse.bms.entity.CommentDO;
import com.scse.bms.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: CommentController
 * @Author: 吴赠禹
 * @Date: 2021/2/25 13:09
 * @Version: 1.0
 * @Description: 评论处理器
 */


@Slf4j
@Controller
public class CommentController {


    @Autowired
    private CommentService commentService;

    @PostMapping("/comment/add")
    @ResponseBody
    public String addComment(@RequestBody CommentDO commentDO) {
        log.info("[addComment] 添加评论，{}", commentDO);
        commentDO.setCreated(DateUtil.now());
        boolean save = commentService.save(commentDO);
        return String.valueOf(save);
    }

    @PostMapping("/comment/delete")
    @ResponseBody
    public String deleteComment(@RequestBody CommentDO commentDO) {
        log.info("[deleteComment] 删除评论，{}", commentDO);
        boolean result = commentService.removeById(commentDO.getId());
        return String.valueOf(result);
    }


    /**
     * 同意审核
     *
     * @param commentDO
     * @return
     */
    @PostMapping("/comment/approve")
    @ResponseBody
    public String updateComment(@RequestBody CommentDO commentDO) {
        log.info("[updateComment] 批准评论，{}", commentDO);
        int agreement = commentService.agreement(commentDO);
        return String.valueOf(agreement);
    }

    @GetMapping("/comment/list")
    @ResponseBody
    public String getCommentList(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false, defaultValue = "6") Integer limit) {
        log.info("[getCommentList] 获取所有评论");
        Page<CommentDO> page = new Page<>(pageNum, limit);
        IPage<CommentDO> list = commentService.list(page);
        long total = list.getTotal();
        List<CommentDO> records = list.getRecords();
        JSONObject bean = new JSONObject();
        bean.put("code", 0);
        bean.put("msg", "success");
        bean.put("count", total);
        bean.put("data", records);
        return bean.toString();
    }


    @PostMapping("/comment/changeStatus")
    @ResponseBody
    public String changeStatus(@RequestBody CommentDO commentDO) {
        log.info("[changeStatus] 修改评论状态，{}", commentDO);
        int result = commentService.changeStatus(commentDO);
        return String.valueOf(result);
    }


    /**
     * 根据bookId获取过审的评论
     */
    @GetMapping("comment/listForUser")
    @ResponseBody
    public String getCommentListForUser(@RequestParam("bookId") Integer bookId) {
        List<CommentVO> list = commentService.listForUserScan(bookId);
        // 使用JSONArray转换列表为JSON格式
        return list.toString();

    }

}
